package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.car.entity.Car;
import cn.stylefeng.guns.modular.car.service.CarService;
import cn.stylefeng.guns.modular.school.entity.ParkingInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceUseInfo;
import cn.stylefeng.guns.modular.school.mapper.ParkingPlaceUseInfoMapper;
import cn.stylefeng.guns.modular.school.model.params.ParkingPlaceUseInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ParkingPlaceUseInfoResult;
import cn.stylefeng.guns.modular.school.service.ParkingInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceUseInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 停车位使用情况信息 服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-15
 */
@Service
public class ParkingPlaceUseInfoServiceImpl extends ServiceImpl<ParkingPlaceUseInfoMapper, ParkingPlaceUseInfo> implements ParkingPlaceUseInfoService {

    @Autowired
    private CarService carService;
    @Autowired
    private ParkingPlaceInfoService parkingPlaceInfoService;
    @Autowired
    private ParkingInfoService parkingInfoService;

    @Override
    public void add(ParkingPlaceUseInfoParam param) {
        ParkingPlaceUseInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ParkingPlaceUseInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ParkingPlaceUseInfoParam param) {
        ParkingPlaceUseInfo oldEntity = getOldEntity(param);
        ParkingPlaceUseInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ParkingPlaceUseInfoResult findBySpec(ParkingPlaceUseInfoParam param) {
        return null;
    }

    @Override
    public ParkingPlaceUseInfoResult sumByParkingPlaceId(ParkingPlaceUseInfoParam param) {
        return this.baseMapper.sumByParkingPlaceId(param);
    }

    @Override
    public List<ParkingPlaceUseInfoResult> findListBySpec(ParkingPlaceUseInfoParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ParkingPlaceUseInfoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<ParkingPlaceUseInfoResult> records = page.getRecords();
        records.forEach(parkingPlaceUseInfoResult -> {
            Car car = carService.getOne(new QueryWrapper<Car>().eq("select_default", "1").eq("open_id", parkingPlaceUseInfoResult.getOpenId()), false);
            parkingPlaceUseInfoResult.setCarNo(car == null ? "" : car.getNo());
            ParkingPlaceInfo parkingPlaceInfo = parkingPlaceInfoService.getById(parkingPlaceUseInfoResult.getParkingPlaceId());
            if(parkingPlaceInfo == null){
                parkingPlaceInfo = new ParkingPlaceInfo();
            }
            parkingPlaceUseInfoResult.setLocation(parkingPlaceInfo.getLocation());
            ParkingInfo parkingInfo = parkingInfoService.getById(parkingPlaceInfo.getParkingId());
            parkingPlaceUseInfoResult.setParkingName(parkingInfo == null ? "" : parkingInfo.getName());
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ParkingPlaceUseInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ParkingPlaceUseInfo getOldEntity(ParkingPlaceUseInfoParam param) {
        return this.getById(getKey(param));
    }

    private ParkingPlaceUseInfo getEntity(ParkingPlaceUseInfoParam param) {
        ParkingPlaceUseInfo entity = new ParkingPlaceUseInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
